<template>
  <el-dialog
    title="收货地址"
    :title="options.title"
    :visible.sync="options.visible">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="标签名称" :label-width="formLabelWidth" prop="name">
        <!-- <el-input v-model="form.name" autocomplete="off"></el-input> -->
        <el-cascader
          v-model="selectedPath"
          :options="iTagOptions"
          :props="{ checkStrictly: true }"
          clearable
          @change="handleChange"></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      options: {
        type: Object,
        default() {
          return {};
        },
      },
      row: {
        type: Object,
        default() {
          return null;
        },
      },
    },
    data() {
      return {
        form: {},
        rules: {
          classname: [
            { required: true, message: "请输入算子类名", trigger: "blur" },
          ],
          // name: [
          //   { required: true, message: "请输入算子名称", trigger: "blur" },
          // ],
          description: [
            { required: true, message: "请输入描述", trigger: "blur" },
          ],
          params: [{ required: true, message: "请输入参数", trigger: "blur" }],
          // path: [
          //   { required: true, message: "请输入路径", trigger: "blur" },
          //   // {
          //   //   min: 1,
          //   //   max: 18,
          //   //   message: "长度在 1 到 18 个字符",
          //   //   trigger: "blur",
          //   // },
          // ],
          type: [
            {
              required: true,
              message: "请至少选择一个类型",
              trigger: "change",
            },
          ],
        },
        formLabelWidth: "100px",
        filePath: "",
        filetext: "",
        selectedPath: [],
        iTagOptions: [
          {
            value: "父级一类1",
            label: "父级一类1",
            children: [
              {
                value: "父级二类1",
                label: "父级二类1",
                children: [
                  {
                    value: "父级三类1",
                    label: "父级三类1",
                  },
                ],
              },
              {
                value: "父级二类2",
                label: "父级二类2",
                children: [
                  {
                    value: "父级三类1",
                    label: "父级三类1",
                  },
                ],
              },
            ],
          },
          {
            value: "父级一类2",
            label: "父级一类2",
            children: [
              {
                value: "父级二类1",
                label: "父级二类1",
                children: [
                  {
                    value: "父级三类1",
                    label: "父级三类1",
                  },
                ],
              },
            ],
          },
          {
            value: "父级一类3",
            label: "父级一类3",
            children: [
              {
                value: "父级二类1",
                label: "父级二类1",
              },
            ],
          },
        ],
      };
    },

    created() {
      this.init();
    },
    methods: {
      selectFile() {
        this.$refs.fileInput.click();
      },
      handleFileUpload(event) {
        const file = event.target.files[0];
        if (!file) {
          alert("请选择一个文件");
          return;
        }
        this.filePath = file.name;
        console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        let result = window.iOperator.upload(this.filePath);
        let res = JSON.parse(result);
        this.filetext = res.data;
        this.form.path = res.data;
        // if (code == 1) {
        //   this.$message.success(res.msg);
        // } else {
        //   this.$message.error(res.msg);
        // }
        this.$refs.fileInput.value = null;

        // 检查文件类型是否为 JSON
        // const fileExtension = file.name.split(".").pop().toLowerCase();
        // if (fileExtension !== "json") {
        //   this.$message.error("只允许上传JSON格式的文件");
        //   return;
        // } else {
        //   this.filePath = file.name;
        //   this.form.path = 111;
        //   console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        //   // const fileName = file.name.split("\\").pop();
        //   // this.fileName = fileName.split(".")[0];
        //   // let item = {
        //   //   // key: this.fileName2,
        //   //   key: "MetaB",
        //   //   path: this.filePath2,
        //   // };
        //   // let itemAdd = JSON.stringify(item);
        //   // let result = window.iTest.upload(itemAdd);
        //   // let res = JSON.parse(result);
        //   // if (res.code == 1) {
        //   //   this.$message.success(res.msg);
        //   // } else {
        //   //   this.$message.error(res.msg);
        //   // }
        //   // this.$refs.fileInput2.value = null;
        // }
      },
      init() {
        if (this.row) {
          this.form = JSON.parse(JSON.stringify(this.row));
          console.log("🚀 ~ init ~ this.form:", this.form);
        }
      },
      confirm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.row
              ? this.$emit("onEditClose", this.form)
              : this.$emit("onAddClose", this.form);
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      close() {
        this.row ? this.$emit("onEditClose") : this.$emit("onAddClose");
      },
      handleChange(value) {
        // console.log("🚀 ~ handleChange ~ valve:", value);
        if (value && value.length > 0) {
          this.form.name = value[value.length - 1];
        } else {
          this.form.name = "";
        }
      },
    },
  };
</script>

<style scoped>
  .el-dialog {
    /* width: 500px !important; */
    padding-right: 30px;
  }
</style>
